package com.sino.config.server.controller.project;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.nutz.dao.Cnd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sino.config.server.ConfigServerApplication.Consts.InstalledPermission;
import com.sino.config.server.ConfigServerApplication.Consts.InstalledRole;
import com.sino.config.server.bean.project.Application;
import com.sino.config.server.controller.BaseController;
import com.sino.config.server.ext.anno.SINORequiresPermissions;
import com.sino.config.server.service.acl.ShiroUserService;
import com.sino.config.server.service.project.ApplicationMemberService;
import com.sino.config.server.service.project.ApplicationService;

import club.zhcs.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author kerbores
 *
 */
@RestController
@RequestMapping("application")
@Api(value = "Application", tags = { "Application" })
public class ApplicationController extends BaseController {

	@Autowired
	ApplicationService applicationService;

	@Autowired
	ApplicationMemberService applicationMemberService;

	@Autowired
	ShiroUserService shiroUserService;

	/**
	 * 
	 * @param page
	 * @return
	 */
	@GetMapping("list")
	@SINORequiresPermissions(InstalledPermission.APPLICATION_LIST)
	@ApiOperation("Application列表")
	public Result list(@RequestParam(value = "page", defaultValue = "1") @ApiParam("页码") int page) {
		if (!shiroUserService.checkRole(InstalledRole.SU.getName(), SecurityUtils.getSubject().getPrincipal().toString())) { // manager
			return Result.success().addData("pager", applicationService.listByUser(_fixPage(page)));
		}
		return Result.success().addData("pager", applicationService.searchByPage(_fixPage(page), Cnd.NEW().desc("id")));
	}

	/**
	 * 
	 * @param key
	 * @param page
	 * @return
	 */
	@GetMapping("search")
	@SINORequiresPermissions(InstalledPermission.APPLICATION_LIST)
	@ApiOperation("Application分页搜索")
	public Result search(@RequestParam("key") String key, @RequestParam(value = "page", defaultValue = "1") int page) {
		return Result.success().addData("pager", applicationService.searchByKeyAndPage(_fixSearchKey(key), _fixPage(page), "name", "description").addParam("key", key));
	}

	/**
	 * 
	 * @param Application
	 * @return
	 */
	@PostMapping("add")
	@SINORequiresPermissions(InstalledPermission.APPLICATION_ADD)
	@ApiOperation("Application新增")
	public Result save(@RequestBody Application application) {
		return applicationService.save(application) == null ? Result.fail("保存Application失败!") : Result.success().addData("application", application);
	}

	/**
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("{id}")
	@SINORequiresPermissions(value = { InstalledPermission.APPLICATION_EDIT })
	@ApiOperation("Application详情")
	public Result detail(@PathVariable("id") @ApiParam("Applicationid") long id) {
		return Result.success().addData("application", applicationService.fetch(id));
	}

	/**
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("delete/{id}")
	@SINORequiresPermissions(value = { InstalledPermission.APPLICATION_DELETE })
	@ApiOperation("删除Application")
	public Result delete(@PathVariable("id") @ApiParam("Applicationid") long id) {
		return applicationService.delete(id) == 1 ? Result.success() : Result.fail("删除Application失败!");
	}

	/**
	 * 
	 * @param Application
	 * @return
	 */
	@PostMapping("edit")
	@SINORequiresPermissions(InstalledPermission.APPLICATION_EDIT)
	@ApiOperation("编辑Application")
	public Result update(@RequestBody Application application) {
		return applicationService.updateIgnoreNull(application) != 1 ? Result.fail("更新Application失败!") : Result.success().addData("application", application);
	}

	/**
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("{id}/manager")
	@SINORequiresPermissions(value = { InstalledPermission.APPLICATION_EDIT, InstalledPermission.APPLICATION_ADD }, logical = Logical.OR)
	@ApiOperation("项目经理信息")
	public Result manager(@PathVariable("id") @ApiParam("applicationId") long id) {
		return Result.success().addData("infos", applicationMemberService.selectManagerInfo(id));
	}
}
